/*
* Copyright (C) 2010-2012 Magnus Skjegstad
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
* 
* The latest version of this library can be downloaded from http://mist-pubsub.googlecode.com
* 
*/
package com.skjegstad.mist2.demos;

import com.skjegstad.mist2.IMistSubscriptionListener;
import com.skjegstad.mist2.MistCoreManager;
import com.skjegstad.mist2.datatypes.DataElement;
import com.skjegstad.mist2.exceptions.MistException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 *
 * @author Magnus Skjegstad
 */
public class TestBroadcastFileTransfer {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException, MistException, UnknownHostException {


        MistCoreManager mist1 = new MistCoreManager(1);
        //mist1.enableMulticast(InetAddress.getByName("224.0.0.1"), 1234);
        mist1.enableBroadcast(InetAddress.getByName("255.255.255.255"), 1234);
        mist1.subscribe("no.ffi", new IMistSubscriptionListener() {
            public void deliverMessage(DataElement message) {
                System.out.println("RECEIVED DATA ELEMENT in node 1");
            }
        });
        mist1.setDebug(true);
        mist1.start();

        MistCoreManager mist2 = new MistCoreManager(2);
        //mist2.enableMulticast(InetAddress.getByName("224.0.0.1"), 1234);
        mist2.enableBroadcast(InetAddress.getByName("255.255.255.255"), 1234);
        mist2.subscribe("no.ffi", new IMistSubscriptionListener() {
            public void deliverMessage(DataElement message) {
                System.out.println("RECEIVED DATA ELEMENT in node 2");
            }
        });
        mist2.setDebug(true);
        mist2.start();

        mist1.publish("no.ffi", new byte[100000]);


        System.out.println("Sleeping.");
        for (int i = 0; i < 120; i++) {
            Thread.sleep(1000);
            System.out.print(".");
        }

        System.out.println("Stopping mist...");
        mist1.stop();
        mist2.stop();

        System.out.println("Node 1 stats:");
        System.out.println("Elements: " + mist1.getDataElements().size());

        System.out.println("Node 2 stats:");
        System.out.println("Elements: " + mist2.getDataElements().size());

        System.out.println("Done..");
    }

}
